package com.example.springevent.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;

@Slf4j
@Configuration
@EnableAsync
public class EventConfig {
    
    /**
     * 配置全局异步事件多播器
     * 注意：这会使所有事件默认异步处理，通常不建议这样做
     * 这里仅作为演示，实际项目中建议使用@Async注解来标记特定的事件处理方法为异步
     */
    @Bean(name = "applicationEventMulticaster")
    public ApplicationEventMulticaster applicationEventMulticaster() {
        log.info("配置全局异步事件多播器");
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor("global-event-"));
        return eventMulticaster;
    }
}